package com.module.antiFake.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.antiFake.dao.AntiFakeComplainDao;
import com.module.antiFake.dao.AntiFakeReplyDao;
import com.module.antiFake.util.AntiFakeBeanUtil;
import com.module.antiFake.util.AntiFakeConstant;
import com.module.antiFake.vo.AntiFakeComplain;
import com.module.antiFake.vo.AntiFakeReply;

public class AntiFakeReplyManage extends GenericHibernateManager<AntiFakeReply, Integer> {
	public static AntiFakeReplyManage service;
	private AntiFakeReplyDao dao = AntiFakeReplyDao.getInstance();
	private AntiFakeComplainDao complainDao = AntiFakeComplainDao.getInstance();
	private AntiFakeReplyManage(){
		
	}
	public synchronized static AntiFakeReplyManage getInstance(){
		if(service == null){
			service = new AntiFakeReplyManage();
		}
		return service;
	}
	
	
	public List<AntiFakeReply> findByFkId(Integer fkid) throws Exception{
		Session session = this.getSession();
		try {
			return dao.findByFkId(session,fkid);
		} catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}
	}
	/**
	 * 投诉反馈列表
	 * @param antiFakeReply
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public Page listAntiFakeReply(AntiFakeReply antiFakeReply,Page page) throws Exception{
		Session session = this.getSession();
		try {
			page = dao.listAntiFakeReply(session,antiFakeReply, page.getStart(), page.getLimit());
		} catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}
		return page;
	}
	/**
	 * 保存处理信息
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public boolean addAntiFakeReply(AntiFakeReply bean) throws Exception{
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		boolean flag = false;
		try{
			AntiFakeComplain antiFakeComplain=complainDao.get(session,bean.getFkId());
			bean.setReplyName(antiFakeComplain.getComplainName());
			flag = dao.save(session,bean);
			tx.commit();
		}catch (Exception e) {
			tx.rollback();
			throw e;
		}finally{
			this.closeSession();
		}
		return flag; 
	} 
	/**
	 * 企业回复信息
	 * @param bean
	 * @param state
	 * @return
	 * @throws Exception
	 */
	public Map addAntiFakeReply(AntiFakeReply bean,String state) throws Exception{
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		boolean flag = false;
		Map result = new HashMap();
		try {
			AntiFakeComplain antiFakeComplain=complainDao.get(session,bean.getFkId());
			if(antiFakeComplain.getState().equals(AntiFakeConstant.END_STATE)){
				result.put("state", false);
				result.put("msg", "已处理完毕的投诉不能继续回复,请选择其他的数据!");
				return result;
			}
			flag = dao.addAntiFakeReply(session,bean,state);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally{
			this.closeSession();
		}
		result.put("state", false);
		result.put("msg", "保存成功!");
		return result;
	}
	/**
	 * 取下一条投诉反馈记录
	 * @param antiFakeReply
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public Page getNextAntiFakeReply(AntiFakeReply antiFakeReply,Page page) throws Exception{
		Session session = this.getSession();
		try {
			Integer fkId = complainDao.getAntiFakeComplainByOrgId(session,antiFakeReply.getFkId(),antiFakeReply.getOrgId());
			antiFakeReply.setFkId(fkId);
			page = dao.listAntiFakeReply(session,antiFakeReply,page.getStart(),page.getLimit());
		} catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}
		return page;
	}
	
	@Override
	public GenericHibernateDao<AntiFakeReply, Integer> getDao() {
		return dao;
	}
	
}
